package com.zwz.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zwz.shiro.chainDefinitionsFilter.ChainDefinitionsFilterService;

@Controller
@RequestMapping("/test")
public class UserTestAction extends BaseController {
	@Autowired
	private ChainDefinitionsFilterService filterChainDefinitionsService;
	
	@RequestMapping(value="/get")
	public String getUserById(HttpServletRequest request){
		System.out.println("进来了");
		String userName=request.getParameter("userName");
		String passWord=request.getParameter("passWord");
		try{
			UsernamePasswordToken token = new UsernamePasswordToken(userName,passWord);
			Subject user = SecurityUtils.getSubject();
			user.login(token);
		} catch (UnknownAccountException e) {
			System.out.println("账号不存在");
        } catch (DisabledAccountException e) {
        	System.out.println("账号未启用");
        } catch (IncorrectCredentialsException e) {
        	System.out.println("密码错误");
        } catch (Exception e) {
        	System.out.println("未知错误,请联系管理员");
        }
		return "redirect:/resource/tree.do";
	}
	
	@RequestMapping(value="get1")
	public String get1(){
		System.out.println("==========get1========");
		return "success";
	}
	@RequestMapping(value="get2")
	public String get12(){
		System.out.println("==========get2========");
		return "success";
	}
	@RequestMapping(value="get3")
	public String get3(){
		System.out.println("==========get3========");
		return "success";
	}
	@RequestMapping(value="get4")
	public String get4(){
		System.out.println("==========get4========");
		return "success";
	}
	@RequestMapping(value="clear")
	public String clear(){
		System.out.println("==========clear========");
		System.out.println(filterChainDefinitionsService+"初始化成功");
		filterChainDefinitionsService.updatePermission();
		return "success";
	}
	@RequestMapping(value="success")
	public String success(){
		System.out.println("==========success========");
		return "index";
	}
	
	@RequestMapping(value="noty")
	@ResponseBody
	public Object getTest(@RequestBody(required=true) String body){
		System.out.println(body);
		return sendFailureMessage("操作成功");
	}
}
